﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class MaterialSearchTreeHander : IRequestHandler<MaterialSearchTreeCommand, ResultT<MaterialTreeDto>>
    {


        private readonly IBaseRepositorys<Product> productRepository;
        private readonly IBaseRepositorys<Material> materialRepository;

        public MaterialSearchTreeHander(IBaseRepositorys<Product> productRepository, IBaseRepositorys<Material> materialRepository)
        {
            this.productRepository = productRepository;
            this.materialRepository = materialRepository;
        }

        /// <summary>
        /// 获取物料父级Id
        /// </summary>
        /// <param name="request">获取物料父级Id请求对象</param>
        /// <param name="cancellationToken">允许取消异步操作</param>
        /// <returns>返回父级物料id响应对象</returns>
        public async Task<ResultT<MaterialTreeDto>> Handle(MaterialSearchTreeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultT<MaterialTreeDto>();

                var list1 = (from a in materialRepository.GetAll().Where(x => x.ParentId == 0 && !x.DeleteDate)
                             join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                             select new
                             {
                                 a.ProductId,
                                 a.MaterialId,
                                 a.UnitMach,
                                 a.ParentId,
                                 a.CreateId,
                                 a.CreateDate,
                                 a.CreateName,
                                 a.UpdateId,
                                 a.UpdateName,
                                 a.UpdateDate,
                                 b.ProductNumber,
                                 b.ProductName,
                                 b.ProductSpecification,
                                 b.StockUnit,
                                 ParentName = ""
                             }).ToList();


                var result1 = list1.Select(c => new MaterialTreeDto
                {
                    Id = c.MaterialId,
                    Title = c.ProductName,
                    ParentId = c.ProductId,
                    Children = GetMaterialListDtos(c.MaterialId).Count() > 0 ? GetMaterialListDtos(c.MaterialId) : null
                });



                result.Data = result1.ToList();
                return result;
            }
            catch (Exception ex)
            {
                throw;
            }
        }


        /// <summary>
        /// 递归获取物料列表
        /// </summary>
        /// <param name="Pid">父级id</param>
        /// <returns>返回树形物料信息</returns>
        private List<MaterialTreeDto> GetMaterialListDtos(int Pid)
        {
            var list = (from a in materialRepository.GetAll().Where(x => x.ParentId == Pid && !x.DeleteDate)
                        join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                        join c in productRepository.GetAll() on a.ParentId equals c.ProductId
                        select new
                        {
                            a.ProductId,
                            a.MaterialId,
                            a.UnitMach,
                            a.ParentId,
                            a.CreateId,
                            a.CreateDate,
                            a.CreateName,
                            a.UpdateId,
                            a.UpdateName,
                            a.UpdateDate,
                            b.ProductNumber,
                            b.ProductName,
                            b.ProductSpecification,
                            b.StockUnit,
                            ParentNames = c.ProductName
                        }).ToList();


            var result = list.Select(c => new MaterialTreeDto
            {
                Id = c.MaterialId,
                Title = c.ProductName,
                ParentId = c.ProductId,
                Children = GetMaterialListDtos(c.MaterialId).Count() > 0 ? GetMaterialListDtos(c.MaterialId) : null
            }).Distinct().ToList();
            return result;
        }



    }
}
